package com.wjc.wiki.controller;

import com.alibaba.fastjson.JSONObject;
import com.wjc.wiki.req.WikiuserLoginReq;
import com.wjc.wiki.req.WikiuserQueryReq;
import com.wjc.wiki.req.WikiuserResetPasswordReq;
import com.wjc.wiki.req.WikiuserSaveReq;
import com.wjc.wiki.resp.CommonResp;
import com.wjc.wiki.resp.PageResp;
import com.wjc.wiki.resp.WikiuserLoginResp;
import com.wjc.wiki.resp.WikiuserQueryResp;
import com.wjc.wiki.service.WikiuserService;
import com.wjc.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class WikiuserController {

    @Resource
    private WikiuserService wikiuserService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SnowFlake snowFlake;


    private static final Logger LOG= LoggerFactory.getLogger(WikiuserController.class);



    @GetMapping("/list")
    public CommonResp list(@Valid  WikiuserQueryReq req){
        PageResp<WikiuserQueryResp> list=wikiuserService.list(req);
        CommonResp<PageResp<WikiuserQueryResp>> resp=new CommonResp<>();
        resp.setContent(list);
        return  resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody  WikiuserSaveReq req){
       req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp=new CommonResp<>();
        wikiuserService.save(req);
        return  resp;
    }

    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody WikiuserResetPasswordReq req){
       req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp=new CommonResp<>();
        wikiuserService.resetPasswird(req);
        return  resp;
    }

    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody WikiuserLoginReq req){
       req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));//md5加密
        CommonResp<WikiuserLoginResp> resp=new CommonResp<>();
        WikiuserLoginResp wikiuserLoginResp =wikiuserService.login(req);

        Long token=snowFlake.nextId();//使用long包装类可以直接toString()
        LOG.info("生成单点登录token{}，放入redis中",token);
        wikiuserLoginResp.setToken(token.toString());
        //存入redis，24小时有效
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(wikiuserLoginResp),3600*24, TimeUnit.SECONDS);
        resp.setContent(wikiuserLoginResp);
        return  resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp=new CommonResp<>();
        wikiuserService.delete(id);
        return  resp;
    }

    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token){
        CommonResp resp=new CommonResp<>();
        redisTemplate.delete(token);
        LOG.info("后端删除token{}",token);
        return  resp;
    }
}
